<template>
  <div class="main">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="page" class="demo-form-inline" size="mini" style="margin-top: 20px">
      <el-form-item label="博客标题">
        <el-input v-model="page.params.blogTitle" placeholder="博客标题" clearable />
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="page.params.typeId" placeholder="分类" clearable filterable>
          <el-option v-for="item in typeList" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" sizi="mini" @click="getBlogByPage">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 我是分割线 -->
    <!-- <el-divider /> -->
    <el-button type="primary" size="mini" class="add-btn" @click="addBlog">新增博客</el-button>
    <el-table :data="page.list" border style="width: 100%" @sort-change="changeSort">
      <el-table-column prop="blogTitle" label="标题" show-overflow-tooltip />
      <el-table-column prop="typeName" label="分类" />
      <el-table-column prop="blogImage" label="封面" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 100%;height: 100px"
            :src="scope.row.blogImage"
            :preview-src-list="[scope.row.blogImage]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="blogGoods" label="阅读数" width="100" sortable="custom" />
      <el-table-column prop="blogCollection" label="收藏数" width="100" sortable="custom" />
      <el-table-column prop="blogComment" label="评论数" width="100" sortable="custom" />
      <el-table-column prop="blogSource" label="文章来源" />
      <el-table-column prop="createdTime" label="创建时间" width="160" sortable="custom" />
      <el-table-column prop="updateTime" label="修改时间" width="160" sortable="custom" />
      <el-table-column prop="blogRemark" label="备注" show-overflow-tooltip />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleRead(scope.row.blogId)">阅读</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="warning" @click="handleEdit(scope.row.blogId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.blogId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <div v-if="showAdd">
      <el-dialog
        title="新增博客"
        :visible.sync="showAdd"
      >
        <blog-add @getBlogByPage="getBlogByPage" @closeAddBlogForm="closeAddBlogForm" />
      </el-dialog>
    </div>

    <el-dialog
      title="修改博客"
      :visible.sync="showUpdate"
    >
      <blog-update :blog="blog" @getBlogByPage="getBlogByPage" @closeUpdateBlogForm="closeUpdateBlogForm" />
    </el-dialog>

    <el-dialog
      title="阅读博客"
      :visible.sync="showRead"
      width="50%"
    >
      <div v-html="blog.blogContent" />
    </el-dialog>
  </div>
</template>

<script>
import blogApi from '@/api/blog'
import BlogAdd from './blog-add'
import BlogUpdate from './blog-update'
export default {
  components: {
    BlogAdd,
    BlogUpdate
  },
  data() {
    return {
      showAdd: false, // 控制添加博客弹窗是否打开
      showUpdate: false, // 控制分类修改弹窗是否打开
      showRead: false, // 控制阅读窗口是否打开
      blogList: [],
      blog: {
        blogId: '',
        blogTitle: '',
        blogImage: null,
        blogContent: null,
        typeName: '',
        blogSource: null,
        blogRemark: null
      },
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [], // 数据
        sortColumn: '', // 排序列
        sortMethod: 'asc' // 排序方式
      },
      typeList: []
    }
  },
  created() {
    this.getBlogByPage()
    setTimeout(() => {
      this.typeList = this.$store.getters.typeList
    }, 500)
  },
  methods: {
    getBlogByPage() {
      blogApi.getByPage(this.page).then(resp => {
        console.log(resp.data)
        this.page = resp.data
      })
    },
    handleRead(id) {
      blogApi.read(id).then(resp => {
        this.blog = resp.data
        this.showRead = true
      })
    },
    handleEdit(id) {
      // 修改
      blogApi.getById(id).then(resp => {
        this.blog = resp.data
        this.showUpdate = true
      })
    },
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除该博客?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        blogApi.deleteById(id)
        this.$message.success('删除成功!')
        this.getBlogByPage()
      })
    },
    addBlog() {
      this.showAdd = true
    },
    closeAddBlogForm() {
      this.showAdd = false
    },
    closeUpdateBlogForm() {
      this.showUpdate = false
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getBlogByPage()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getBlogByPage()
    },
    changeSort(e) {
      if (e.order) {
        this.page.sortColumn = e.prop
        this.page.sortMethod = e.order
      } else {
        this.page.sortColumn = ''
        this.page.sortMethod = 'asc'
      }
      this.getBlogByPage()
    }
  }
}
</script>
<style scoped>
  .add-btn {
    margin: 5px 0px 5px 0px;
  }
</style>
